package com.ruoyi.mes.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.SecurityUtils;
import com.ruoyi.common.core.utils.bean.ConvertUtils;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.mes.remote.WmsItemFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.mes.domain.MesProcess;
import com.ruoyi.mes.service.IMesProcessService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;

/**
 * 工艺规程Controller
 * 
 * @author ruoyi
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/process")
public class MesProcessController extends BaseController
{
    @Autowired
    private IMesProcessService mesProcessService;

    @Autowired
    private WmsItemFeignClient wmsItemFeignClient;

    /**
     * 查询工艺规程列表
     */
    @PreAuthorize(hasPermi = "mes:process:list")
    @GetMapping("/list")
    public TableDataInfo list(MesProcess mesProcess)
    {
        startPage();
        List<MesProcess> list = mesProcessService.selectMesProcessList(mesProcess);
        return getDataTable(list);
    }
    /**
     * 查询工艺规程列表
     */
    @PreAuthorize(hasPermi = "mes:process:list")
    @GetMapping("/listTree")
    public TableDataInfo listTree(MesProcess mesProcess)
    {
        startPage();
        List<MesProcess> list = mesProcessService.selectMesProcessTreeList(mesProcess);
        return getDataTable(list);
    }

    /**
     * 查询工艺规程列表
     */
    @PreAuthorize(hasPermi = "mes:process:list")
    @GetMapping("/listVersionNum")
    public TableDataInfo listVersionNum(MesProcess mesProcess)
    {
        startPage();
        List<MesProcess> list = mesProcessService.listVersionNum(mesProcess);
        return getDataTable(list);
    }


    /**
     * 获取部门下拉树列表
     */
    @GetMapping("/treeSelect")
    public AjaxResult treeSelect(MesProcess process)
    {
        List<MesProcess> processes = mesProcessService.selectMesProcessList(process);
        return AjaxResult.success(mesProcessService.buildProcessTreeSelect(processes));
    }


    /**
     * 导出工艺规程列表
     */
    @PreAuthorize(hasPermi = "mes:process:export")
    @Log(title = "工艺规程", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesProcess mesProcess) throws IOException
    {
        List<MesProcess> list = mesProcessService.selectMesProcessList(mesProcess);
        ExcelUtil<MesProcess> util = new ExcelUtil<MesProcess>(MesProcess.class);
        util.exportExcel(response, list, "工艺规程数据");
    }

    /**
     * 获取工艺规程详细信息
     */
    @PreAuthorize(hasPermi = "mes:process:query")
    @GetMapping(value = "/{processUuid}")
    public AjaxResult getInfo(@PathVariable("processUuid") String processUuid)
    {
        MesProcess mp = mesProcessService.selectMesProcessByProcessUuid(processUuid);
        return AjaxResult.success(mp);
    }



    /**
     * 新增工艺规程
     */
    @PreAuthorize(hasPermi = "mes:process:add")
    @Log(title = "工艺规程", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody MesProcess mesProcess)
    {
//        TableDataInfo tableDataInfo = wmsItemFeignClient.list("000f07f5-78ea-11ec-9f0f-38d547791b0b");
//        System.out.println(tableDataInfo);
        int i = mesProcessService.insertMesProcess(mesProcess);
        return toAjax( i );
    }


    /**
     * 复制工艺规程
     */
    @PreAuthorize(hasPermi = "mes:process:add")
    @Log(title = "工艺规程", businessType = BusinessType.INSERT)
    @PostMapping("/copy")
    @Transactional
    public AjaxResult copy(@RequestBody MesProcess mesProcess)
    {
        int i = mesProcessService.copyProcess(mesProcess);
        return toAjax( i );
    }

    /**
     * 修改工艺规程
     */
    @PreAuthorize(hasPermi = "mes:process:edit")
    @Log(title = "工艺规程", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesProcess mesProcess)
    {
        int a = mesProcessService.updateMesProcess(mesProcess);
        return toAjax(a);
    }

    /**
     * 删除工艺规程
     */
    @PreAuthorize(hasPermi = "mes:process:remove")
    @Log(title = "工艺规程", businessType = BusinessType.DELETE)
	@DeleteMapping("/{processUuids}")
    @Transactional
    public AjaxResult remove(@PathVariable String[] processUuids)
    {
        return toAjax(mesProcessService.deleteMesProcessByProcessUuids(processUuids));
    }
}
